package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcEverydayDisinfectionRecordMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcDisinfectionPosition;
import com.csun.cmny.provider.model.domain.OmcDisinfectionRecordPositionBind;
import com.csun.cmny.provider.model.domain.OmcEverydayDisinfectionRecord;
import com.csun.cmny.provider.model.dto.disinfectionPosition.EverydayDisinfectionRecordSaveDto;
import com.csun.cmny.provider.model.dto.disinfectionPosition.EverydayDisinfectionRecordSelectDto;
import com.csun.cmny.provider.model.dto.disinfectionPosition.EverydayDisinfectionRecordUpdateDto;
import com.csun.cmny.provider.model.vo.disinfectionPosition.EverydayDisinfectionRecordVo;
import com.csun.cmny.provider.model.vo.disinfectionPosition.HuaLongEveryDsftRecordVo;
import com.csun.cmny.provider.service.OmcDisinfectionPositionService;
import com.csun.cmny.provider.service.OmcDisinfectionRecordPositionBindService;
import com.csun.cmny.provider.service.OmcEverydayDisinfectionRecordService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OmcEverydayDisinfectionRecordServiceImpl extends BaseService<OmcEverydayDisinfectionRecord> implements OmcEverydayDisinfectionRecordService {

    @Resource
    private OmcDisinfectionRecordPositionBindService bindService;

    @Resource
    private OmcDisinfectionPositionService positionService;

    @Resource
    private OmcEverydayDisinfectionRecordMapper everydayDisinfectionRecordMapper;


    @Override
    public int addEverydayDisinfectionRecord(EverydayDisinfectionRecordSaveDto everydayDisinfectionRecordSaveDto, LoginAuthDto loginAuthDto) {

        //校验本楼栋本日期是否已经有消毒记录
        OmcEverydayDisinfectionRecord omcEverydayDisinfectionRecord = new OmcEverydayDisinfectionRecord();
        omcEverydayDisinfectionRecord.setBuildingNo(everydayDisinfectionRecordSaveDto.getBuildingNo());
        omcEverydayDisinfectionRecord.setDisinfectionDate(everydayDisinfectionRecordSaveDto.getDisinfectionDate());
        List<OmcEverydayDisinfectionRecord> select = select(omcEverydayDisinfectionRecord);

        if (PublicUtil.isNotEmpty(select)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032040);
        }

        //写入消毒记录
        BeanUtils.copyProperties(everydayDisinfectionRecordSaveDto,omcEverydayDisinfectionRecord);
        omcEverydayDisinfectionRecord.setGroupId(loginAuthDto.getGroupId());
        omcEverydayDisinfectionRecord.setNursingUserId(loginAuthDto.getUserId());
        omcEverydayDisinfectionRecord.setNursingName(loginAuthDto.getUserName());
        int save = save(omcEverydayDisinfectionRecord);

        //写入消毒记录和消毒位置的绑定关系
        LinkedList<OmcDisinfectionRecordPositionBind> binds = new LinkedList<>();
        for (Long positionId : everydayDisinfectionRecordSaveDto.getPositionIds()) {
            OmcDisinfectionPosition omcDisinfectionPosition = positionService.selectByKey(positionId);

            if (PublicUtil.isEmpty(omcDisinfectionPosition)){
                throw new OmcBizException(ErrorCodeEnum.OMC10032041,positionId);
            }
            OmcDisinfectionRecordPositionBind omcDisinfectionRecordPositionBind = new OmcDisinfectionRecordPositionBind();
            omcDisinfectionRecordPositionBind.setPositionId(positionId);
            omcDisinfectionRecordPositionBind.setPositionName(omcDisinfectionPosition.getPositionName());
            omcDisinfectionRecordPositionBind.setRecordId(omcEverydayDisinfectionRecord.getId());

            binds.add(omcDisinfectionRecordPositionBind);
        }

        int saves=bindService.saveBinds(binds);
        return save;
    }

    @Override
    public List<EverydayDisinfectionRecordVo> selectEverydayDisinfectionRecord(EverydayDisinfectionRecordSelectDto everydayDisinfectionRecordSelectDto) {

        List<EverydayDisinfectionRecordVo> everydayDisinfectionRecordVos=everydayDisinfectionRecordMapper.selectEverydayDisinfectionRecord(everydayDisinfectionRecordSelectDto);

        for (EverydayDisinfectionRecordVo everydayDisinfectionRecordVo : everydayDisinfectionRecordVos) {
            OmcDisinfectionRecordPositionBind omcDisinfectionRecordPositionBind = new OmcDisinfectionRecordPositionBind();
            omcDisinfectionRecordPositionBind.setRecordId(everydayDisinfectionRecordVo.getId());

            List<OmcDisinfectionRecordPositionBind> select = bindService.select(omcDisinfectionRecordPositionBind);

            everydayDisinfectionRecordVo.setPositions(select);
        }


        return everydayDisinfectionRecordVos;
    }

    @Override
    public int updateEverydayDisinfectionRecord(EverydayDisinfectionRecordUpdateDto everydayDisinfectionRecordUpdateDto) {

        OmcDisinfectionRecordPositionBind omcDisinfectionRecordPositionBind = new OmcDisinfectionRecordPositionBind();
        omcDisinfectionRecordPositionBind.setRecordId(everydayDisinfectionRecordUpdateDto.getRecordId());

        bindService.delete(omcDisinfectionRecordPositionBind);

        //写入消毒记录和消毒位置的绑定关系
        LinkedList<OmcDisinfectionRecordPositionBind> binds = new LinkedList<>();
        for (Long positionId : everydayDisinfectionRecordUpdateDto.getPositionIds()) {
            OmcDisinfectionPosition omcDisinfectionPosition = positionService.selectByKey(positionId);

            if (PublicUtil.isEmpty(omcDisinfectionPosition)){
                throw new OmcBizException(ErrorCodeEnum.OMC10032041,positionId);
            }
            OmcDisinfectionRecordPositionBind disinfectionRecordPositionBind = new OmcDisinfectionRecordPositionBind();
            disinfectionRecordPositionBind.setPositionId(positionId);
            disinfectionRecordPositionBind.setPositionName(omcDisinfectionPosition.getPositionName());
            disinfectionRecordPositionBind.setRecordId(everydayDisinfectionRecordUpdateDto.getRecordId());

            binds.add(disinfectionRecordPositionBind);
        }

        int saves=bindService.saveBinds(binds);
        return saves;
    }

    /**
     * 根据楼栋号和年月查询日常消毒记录
     *
     * @param buildingNo the building no
     * @param month      the month  (yyyy-MM)
     *
     * @return the list.
     */
    @Override
    public List<HuaLongEveryDsftRecordVo> queryBuildingNoAndMonth(String buildingNo, String month) {

        return everydayDisinfectionRecordMapper.queryBuildingNoAndMonth(buildingNo, month);
    }
}
